﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace DataToShow.Network
{
    [Serializable]
    public class M_Agreement
    {

        #region variable

        /// <summary>
        /// 数据内容
        /// </summary>
        public string Message { get; set; }
        /// <summary>
        /// 命令字
        /// </summary>
        public CommandType MsgCommandType { get; set; }


        public string strID = "0000";             //包头
        private string tail = "FF";            //包尾

        /// <summary>
        /// CRC校检
        /// </summary>
        private string strCRC = "CRC";
        #endregion
        #region coustruction method
        public M_Agreement(string strID, CommandType commandType, string message)
        {
            MsgCommandType = commandType;
            Message = message;
            strID = strID.PadLeft(4, '0');
            strCRC = "CRC";
        }

        public M_Agreement(CommandType commandType, string message)
        {
            MsgCommandType = commandType;
            Message = message;
            strID = "0000";
            strCRC = "CRC";
        }
        public M_Agreement()
        { }
        #endregion
        #region private method


        /// <summary>
        /// 获得有效长度
        /// </summary>
        /// <returns></returns>
        private string getHexLength()
        {
            int length = Message.Replace(" ", "").Length + 20;
            string hexlength = length.ToString("X");
            return hexlength.PadLeft(6, '0');

        }

        #endregion
        #region public method
        /// <summary>
        /// 字符串转为16进制
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public string StringToHexString(string message)
        {

            byte[] b = Encoding.UTF8.GetBytes(message);//按照指定编码将string编程字节数组
            string result = string.Empty;
            for (int i = 0; i < b.Length; i++)
            {
                result += Convert.ToString(b[i], 16);
            }
            if (result.IndexOf("FF") != -1)
            {
                StringToHexString(message + " ");
            }
            return result.ToUpper();
        }

        /// <summary>
        /// 16进制转为字符串
        /// </summary>
        /// <param name="hexsString"></param>
        /// <returns></returns>
        public string hexStringToString(string mHex)
        {
            mHex = mHex.Replace(" ", "");
            if (mHex.Length <= 0)
                return "";
            byte[] vBytes = new byte[mHex.Length / 2];
            for (int i = 0; i < mHex.Length - 1; i += 2)
                if (!byte.TryParse(mHex.Substring(i, 2), NumberStyles.HexNumber, null, out vBytes[i / 2]))
                    vBytes[i / 2] = 0;
            return Encoding.UTF8.GetString(vBytes);
        }

        /// <summary>
        /// 获得有完整的16进制协议字符串
        /// </summary>
        /// <returns></returns>
        public string GetCompeteString()
        {

            string command = (((int)MsgCommandType).ToString("X")).PadLeft(2, '0');
            string result = strID + command + Message + strCRC + tail;
            return result;
        }
        /// <summary>
        /// 获得信息的内容
        /// </summary>
        /// <returns></returns>
        public string GetMessage()
        {
            return Message;
        }
        /// <summary>
        /// 获得原始信息
        /// </summary>
        /// <returns></returns>
        public string OriginalMessage()
        {
            return Message;
        }
        /// <summary>
        /// 生成CRC校验
        /// </summary>
        /// <returns></returns>
        public string getCRC()
        {
            string crc = "CRC";
            return crc;
        }
        #endregion

    }
}
